package gui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.toedter.calendar.JCalendar;
import javax.swing.BoxLayout;

import utils.DateUtils;

@SuppressWarnings("serial")
public class DateSumWindow extends JFrame implements ActionListener, WindowListener{
	private static String title = "Domandino - Date";
	private static JPanel centerPanel = new JPanel();
	private static JPanel northPanel = new JPanel();
	private static JPanel southPanel = new JPanel();
	private static JCalendar calendar = new JCalendar();
	private static JButton buttonOk = new JButton("ok");
	private static JButton buttonChange = new JButton("reset date");
	private static JLabel text = new JLabel();
	private static Object lock = new Object();
	private static final JPanel southSubPanel1 = new JPanel();
	private static final JPanel southSubPanel2 = new JPanel();
	private static final JLabel lblCountDown = new JLabel();
	
	public DateSumWindow(Component p, Date date){
		
		super(title);
		this.setResizable(false);
		this.setLocationRelativeTo(p);
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		Container cp = this.getContentPane();
		this.addWindowListener(this);
		
		text.setText(" - ");
		northPanel.add(text);
		cp.add(northPanel, BorderLayout.PAGE_START);
		
		Date today = new Date();
		calendar.setMinSelectableDate(today);
		calendar.setDate(date);
		calendar.setMaxSelectableDate(date);
		calendar.getYearChooser().setVisible(false);
		centerPanel.add(calendar);
		cp.add(centerPanel);
		
		buttonChange.setActionCommand("change");
		buttonChange.addActionListener(this);
		southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.Y_AXIS));
		
				
		lblCountDown.setText(DateUtils.getCountDownHTML(date));
		southSubPanel1.add(lblCountDown);
		southPanel.add(southSubPanel1);
		
		
		southSubPanel2.add(buttonChange);
		
		buttonOk.setActionCommand("ok");
		buttonOk.addActionListener(this);
		southSubPanel2.add(buttonOk);
		southPanel.add(southSubPanel2);
		
		cp.add(southPanel, BorderLayout.SOUTH);
		
		pack();
		setVisible(true);
		
	}

	public void waitFor(){
		synchronized (lock) {
			try {
				lock.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("change")){
			Thread t = new Thread(new Runnable() {				
				public void run() {
					File dateFile = new File(resource.Resource.getDateFilePath());
					dateFile.deleteOnExit();
					JOptionPane.showMessageDialog(null, "restart the program");
					System.exit(0);
				}
			});
			t.start();
			this.dispose();
		}else if(e.getActionCommand().equals("ok")){
			synchronized (lock) {
				dispose();
				lock.notify();
			}	
		}
	}

	@Override
	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent e) {
		synchronized (lock) {
			dispose();
			lock.notify();
			
		}		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
